import React from "react";
import { RegisterBehaviour } from "gg-editor";

const nodeSelectBehavior = page => {
  let startModel = null;
  let endModel = null;
  page.on("hoveranchor:beforeaddedge", ev => {
    const item = ev.item;
    startModel = item.model.modal;
    // console.log(ev.item.model);
  });
  page.on('dragedge:beforeshowanchor', ev => {
    const source = ev.source;
    const sourceId = ev.source.id;
    const target = ev.target;
    const targetId = target.model.id;
    // console.log(source.model,target.model);
    // 每个结点不能连自身
    if(sourceId === targetId){
      ev.cancel = true;
    }
    // 每个结点最多只有一个父节点
    let inEdges = target.getInEdges(); // 输入的边
    // if(inEdges.length > 0){
    //   ev.cancel = true;
    // }
    // 每个结点只有一个子节点，开始节点除外
    let outEdges = source.getOutEdges();
    let isHasGradeNode = false; // 判断是否包含触发条件，否则只能有一个子节点

    // if(outEdges.length >= 1){
      // for(let j = 0, oLength = outEdges.length; j < oLength; j++ ){
      //   if(outEdges[j].target.model.modal === "trigger"){
      //     isHasGradeNode = true;
      //   }
      // }
      // if(!isHasGradeNode){
      //   ev.cancel = true;
      // }
      // ev.cancel = true;
    // }
  });
};

const ExampleBehaviour = () => (
  <RegisterBehaviour name="nodeSelect" behaviour={nodeSelectBehavior} />
);

export default ExampleBehaviour;
